package su.base.module;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import su.security.entity.SecurityUser;
import su.security.service.SecurityUserService;

@Controller
@RequestMapping("/login")
public class LoginServlet {

	@Autowired
	private SecurityUserService securityUserService;
	
	/**
	 * 登录
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("do_login")
	public String login(HttpServletRequest request, Model model){
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		//log.info(username+","+userpwd);
		SecurityUser user = securityUserService.login(username, userpwd);
		if(user==null){
			return "/login.jsp?sumsg=用户名或密码错误";
		}
		SecurityUser sessionUser = new SecurityUser();
		sessionUser.setId(user.getId());
		sessionUser.setUsername(user.getUsername());
		sessionUser.setUsertype(user.getUsertype());
		request.getSession().setAttribute("sessionUser", sessionUser);
		return "redirect:/admin/security/index.action";
	}
	
}
